package com.vitabuybuy.tv.ui.shopcenter.store

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseFragment
import com.library.common.base.MessageModel
import com.library.common.net.loadingtype.LoadingType
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.FragmentShopHomeBiddingBinding
import com.vitabuybuy.tv.dto.TabEntity
import com.vitabuybuy.tv.ui.home.bidd.BiddingCommodityActivity
import com.vitabuybuy.tv.ui.home.bidd.adapter.BiddingZoneAdapter
import com.vitabuybuy.tv.ui.shopcenter.store.viewmodel.StoreHomeViewModel
import kotlinx.android.synthetic.main.fragment_shop_home_bidding.listview
import kotlinx.android.synthetic.main.fragment_shop_home_bidding.refresh
import kotlinx.android.synthetic.main.fragment_shop_home_bidding.tlBiddingClass
import java.util.ArrayList

class StoreHomeBiddingFragment : BaseFragment<StoreHomeViewModel, FragmentShopHomeBiddingBinding>() {
    override val layoutId: Int
        get() = R.layout.fragment_shop_home_bidding
    private var pageNumber = 1
    private var storeId:Int? = -1
    private val biddingAdapter by lazy { BiddingZoneAdapter() }
    private val mTabEntities = ArrayList<CustomTabEntity>()
    private lateinit var groupType: String

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.mViewModel = viewModel
        groupType = getString(R.string.underway)
        listview.apply {
            adapter = biddingAdapter
            layoutManager = LinearLayoutManager(mContext)
        }
        storeId = (parentFragment as StoreHomeFragment).getStoreId()

        mTabEntities.add(TabEntity(getString(R.string.hot_bidding), 0, 0))
        mTabEntities.add(TabEntity(getString(R.string.soon_bidding), 0, 0))
        tlBiddingClass.setTabData(mTabEntities)
    }

    override fun lazyLoadData() {
        viewModel.getBiddingGoodsList(
            storeId,
            groupType,
            pageNumber
        )
    }


    override fun observeData() {
        super.observeData()
        viewModel.biddingGoodslist.observe(this, androidx.lifecycle.Observer {
            if (pageNumber == 1) {
                biddingAdapter.setNewData(it)
            } else {
                biddingAdapter.addData(it)
            }
            if (biddingAdapter.mData.isEmpty()) {
                viewModel.statusView.set(LoadingType.SHOW_EMPTY)
            }else{
                viewModel.statusView.set(LoadingType.SHOW_CONTENT)
            }
        })
        tlBiddingClass.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                biddingAdapter.currentId = position
                groupType = when (position) {
                    0 -> getString(R.string.underway)
                    1 -> getString(R.string.wait_start)
                    else -> getString(R.string.underway)
                }
                pageNumber = 1
                viewModel.getBiddingGoodsList(
                    storeId,
                    groupType,
                    pageNumber
                )
            }

            override fun onTabReselect(position: Int) {
            }
        })
    }

    override fun eventHandling() {
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getBiddingGoodsList(
                    storeId,
                    groupType,
                    pageNumber
                )
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getBiddingGoodsList(
                    storeId,
                    groupType,
                    pageNumber
                )
            }
        })
        biddingAdapter.setOnItemClickListener { _, item, _ ->
            BiddingCommodityActivity.openActivity(mContext,item.product.id,item.id)
        }
    }
    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getBiddingGoodsList(
                    storeId,
                    groupType,
                    pageNumber
                )
            }
            1 -> {
                refresh.onLoad(msg.arg1)
            }
        }
    }


}
